<?php
namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\Complaint;
use yii\data\Pagination;

class ComplaintController extends BaseController
{
    public $modelClass = Complaint::class;

    /**
     * 建议首页
     * @return string
     */
    public function actionIndex()
    {
        //两表相连接
        $models = Complaint::find()->with('type')->all();
        $pages  = new Pagination(['totalCount' => count($models),'pageSize' => $this->pageSize]);
        return $this->render($this->action->id, [
            'models' => $models,
            'pages' => $pages
        ]);
    }

    /**
     * 读取信息
     */
    public function actionRead()
    {
        //获取信息id
        $id = \Yii::$app->request->get('id');
        $read = Complaint::find()->where(['id'=>$id])->one();
        //判断该信息是否为已读
        if($read['status']=='已读'){
            $this->message('该消息已读取', $this->redirect(['index']), 'error');
        }else{
            $read->status = '已读';
            if($read->save()){
                $this->message('读取成功', $this->redirect(['index']), 'success');
//                return \Yii::$app->getSession()->setFlash('success', '读取成功');
            }else{
                $this->message($this->getError($read), $this->redirect(['index']), 'error');
//                return \Yii::$app->getSession()->setFlash('error', '失败');
            }
        }
    }

    /**
     * 信息删除
     * @return mixed|\yii\console\Response|\yii\web\Response
     */
    public function actionCancel()
    {
        $id = \Yii::$app->request->get('id');
        if($id){
            $model = Complaint::findOne($id);
            return $model->delete()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }else{
            $this->message('信息id接收失败', $this->redirect(['index']), 'error');
        }
    }
}